﻿Imports CapaNegocio
Imports CapaEntidad

Public Class ListarClientes
    Inherits Page

    Dim _negocio As New NegocioClientes
    Dim eCliente As New Cliente

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
        txtBuscar.Focus()

        If Not IsPostBack Then
            ListadoClientes()
        End If
    End Sub

    Protected Sub ListadoClientes()
        dvClientes.DataSource = _negocio.ListarClientes(txtBuscar.Text)
        dvClientes.DataBind()
    End Sub

    Protected Sub btnNuevo_Click(sender As Object, e As EventArgs) Handles btnNuevo.Click
        Response.Redirect("~/MantenimientoClientes.aspx")
    End Sub

    Protected Sub btnBuscar_Click(sender As Object, e As EventArgs) Handles btnBuscar.Click
        ListadoClientes()
    End Sub

    Protected Sub dvClientes_RowCommand(sender As Object, e As GridViewCommandEventArgs) Handles dvClientes.RowCommand
        Try
            Dim indiceFila As Short = Convert.ToInt16(e.CommandArgument)
            Dim codCli As String

            If indiceFila >= 0 And indiceFila < dvClientes.Rows.Count Then
                codCli = dvClientes.Rows(indiceFila).Cells(2).Text

                If e.CommandName = "Actualizar" Then
                    Session("CodCli") = codCli
                    Response.Redirect("~/MantenimientoClientes.aspx")
                End If

                If e.CommandName = "Eliminar" Then
                    eCliente.Codigo = codCli
                    eCliente.Estado = 0

                    If _negocio.EliminarCliente(eCliente) Then
                        ListadoClientes()
                    End If
                End If
            End If
        Catch ex As Exception
        End Try
    End Sub

    Protected Sub dvClientes_PageIndexChanging(sender As Object, e As GridViewPageEventArgs) Handles dvClientes.PageIndexChanging
        Try
            dvClientes.PageIndex = e.NewPageIndex
            dvClientes.DataBind()
            ListadoClientes()
        Catch ex As Exception
        End Try
    End Sub
End Class